package coml.jiyun.hp.day14_17and20.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import java.util.ArrayList;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import coml.jiyun.hp.day14_17and20.R;
import coml.jiyun.hp.day14_17and20.bean.BannerBean;
import coml.jiyun.hp.day14_17and20.bean.WanBean;

public class RlvHomeAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<BannerBean.DataBean> bannerBean = new ArrayList<>();
    private ArrayList<WanBean.DataBean.DatasBean> datasBeans;
    private int ban = 0;
    private int rlv = 1;

    public RlvHomeAdapter(Context context, ArrayList<WanBean.DataBean.DatasBean> datasBeans) {
        this.context = context;
        this.bannerBean = bannerBean;
        this.datasBeans = datasBeans;
    }

    public void setBannerBean(List<BannerBean.DataBean> bannerBean) {
        this.bannerBean.addAll(bannerBean);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ban){
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_banner, parent, false);
            return new BannerHolder(inflate);
        }else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.item_rlv, parent, false);
            return new RlvHolder(inflate);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        int itemViewType = holder.getItemViewType();
        if (itemViewType == ban){
            BannerHolder bannerHolder = (BannerHolder) holder;
            bannerHolder.mBan.setImages(bannerBean)
                .setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {

                        Glide.with(context).load(bannerBean.get(position).getImagePath()).into(imageView);
                    }
                }).start();
        }else {
            RlvHolder rlvHolder = (RlvHolder) holder;
            WanBean.DataBean.DatasBean datasBean = datasBeans.get(position);
            Glide.with(context).load(R.mipmap.ic_launcher).apply(new RequestOptions().circleCrop()).into(rlvHolder.mitem_rlv_img);
            rlvHolder.mitem_rlv_tv1.setText(datasBean.getChapterName());
            rlvHolder.mitem_rlv_tv2.setText(datasBean.getTitle());
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return ban;
        }else {
            return rlv;
        }
    }

    @Override
    public int getItemCount() {
        return datasBeans.size();
    }

    public class BannerHolder extends RecyclerView.ViewHolder {

        Banner mBan;

        public BannerHolder(@NonNull View itemView) {
            super(itemView);

            mBan = itemView.findViewById(R.id.item_banner_banner);
        }
    }
    public class RlvHolder extends RecyclerView.ViewHolder {

        ImageView mitem_rlv_img;
        TextView mitem_rlv_tv1;
        TextView mitem_rlv_tv2;


        public RlvHolder(@NonNull View itemView) {
            super(itemView);

            mitem_rlv_img = itemView.findViewById(R.id.item_rlv_img);
            mitem_rlv_tv1 = itemView.findViewById(R.id.item_rlv_tv1);
            mitem_rlv_tv2 = itemView.findViewById(R.id.item1_rlv_tv2);

        }
    }
}
